/**
 * <copyright>
 *
 * Copyright (c) 2011 Zeligsoft (2009) Limited
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Zeligsoft (2009) Limited - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.omg.deployment.parts.impl;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent;
import org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent;
import org.eclipse.emf.eef.runtime.api.parts.ISWTPropertiesEditionPart;
import org.eclipse.emf.eef.runtime.impl.notify.PropertiesEditionEvent;
import org.eclipse.emf.eef.runtime.impl.parts.CompositePropertiesEditionPart;
import org.eclipse.emf.eef.runtime.ui.parts.PartComposer;
import org.eclipse.emf.eef.runtime.ui.parts.sequence.BindingCompositionSequence;
import org.eclipse.emf.eef.runtime.ui.parts.sequence.CompositionSequence;
import org.eclipse.emf.eef.runtime.ui.utils.EditingUtils;
import org.eclipse.emf.eef.runtime.ui.widgets.EEFFeatureEditorDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.omg.deployment.DeploymentPackage;
import org.omg.deployment.parts.DeploymentViewsRepository;
import org.omg.deployment.parts.PlanLocalityPropertiesEditionPart;
import org.omg.deployment.providers.DeploymentMessages;


/**
 * @author Toby McClean; Zeligsoft (2009) Limited
 * @generated
 */
public class PlanLocalityPropertiesEditionPartImpl extends CompositePropertiesEditionPart implements ISWTPropertiesEditionPart, PlanLocalityPropertiesEditionPart {

	protected Text constraint;
	protected Button editConstraint;
	private EList constraintList;



	/**
	 * Default constructor
	 * @param editionComponent the {@link IPropertiesEditionComponent} that manage this part
	 * @generated
	 */
	public PlanLocalityPropertiesEditionPartImpl(IPropertiesEditionComponent editionComponent) {
    super(editionComponent);
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.parts.ISWTPropertiesEditionPart#
	 * 			createFigure(org.eclipse.swt.widgets.Composite)
	 * @generated
	 */
	public Composite createFigure(final Composite parent) {
    view = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;
    view.setLayout(layout);
    createControls(view);
    return view;
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.parts.ISWTPropertiesEditionPart#
	 * 			createControls(org.eclipse.swt.widgets.Composite)
	 * @generated
	 */
	public void createControls(Composite view) { 
    CompositionSequence planLocalityStep = new BindingCompositionSequence(propertiesEditionComponent);
    planLocalityStep
      .addStep(DeploymentViewsRepository.PlanLocality.Properties.class)
      .addStep(DeploymentViewsRepository.PlanLocality.Properties.constraint);
    
    
    composer = new PartComposer(planLocalityStep) {

      @Override
      public Composite addToPart(Composite parent, Object key) {
        if (key == DeploymentViewsRepository.PlanLocality.Properties.class) {
          return createPropertiesGroup(parent);
        }
        if (key == DeploymentViewsRepository.PlanLocality.Properties.constraint) {
          return createConstraintMultiValuedEditor(parent);
        }
        return parent;
      }
    };
    composer.compose(view);
  }

	/**
	 * @generated
	 */
	protected Composite createPropertiesGroup(Composite parent) {
    Group propertiesGroup = new Group(parent, SWT.NONE);
    propertiesGroup.setText(DeploymentMessages.PlanLocalityPropertiesEditionPart_PropertiesGroupLabel);
    GridData propertiesGroupData = new GridData(GridData.FILL_HORIZONTAL);
    propertiesGroupData.horizontalSpan = 3;
    propertiesGroup.setLayoutData(propertiesGroupData);
    GridLayout propertiesGroupLayout = new GridLayout();
    propertiesGroupLayout.numColumns = 3;
    propertiesGroup.setLayout(propertiesGroupLayout);
    return propertiesGroup;
  }

	protected Composite createConstraintMultiValuedEditor(Composite parent) {
		constraint = new Text(parent, SWT.BORDER | SWT.READ_ONLY);
		constraint.setEnabled(false);
		constraint.setToolTipText(DeploymentMessages.PlanLocality_ReadOnly);
		GridData constraintData = new GridData(GridData.FILL_HORIZONTAL);
		constraintData.horizontalSpan = 2;
		constraint.setLayoutData(constraintData);
		EditingUtils.setID(constraint, DeploymentViewsRepository.PlanLocality.Properties.constraint);
		EditingUtils.setEEFtype(constraint, "eef::MultiValuedEditor::field"); //$NON-NLS-1$
		editConstraint = new Button(parent, SWT.NONE);
		editConstraint.setText(DeploymentMessages.PlanLocalityPropertiesEditionPart_ConstraintLabel);
		GridData editConstraintData = new GridData();
		editConstraint.setLayoutData(editConstraintData);
		editConstraint.addSelectionListener(new SelectionAdapter() {

			/**
			 * {@inheritDoc}
			 * 
			 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
			 */
			public void widgetSelected(SelectionEvent e) {
				EEFFeatureEditorDialog dialog = new EEFFeatureEditorDialog(
						constraint.getShell(), "PlanLocality", new AdapterFactoryLabelProvider(adapterFactory), //$NON-NLS-1$
						constraintList, DeploymentPackage.eINSTANCE.getPlanLocality_Constraint().getEType(), null,
						false, true, 
						null, null);
				if (dialog.open() == Window.OK) {
					constraintList = dialog.getResult();
					if (constraintList == null) {
						constraintList = new BasicEList();
					}
					constraint.setText(constraintList.toString());
					propertiesEditionComponent.firePropertiesChanged(new PropertiesEditionEvent(PlanLocalityPropertiesEditionPartImpl.this, DeploymentViewsRepository.PlanLocality.Properties.constraint, PropertiesEditionEvent.COMMIT, PropertiesEditionEvent.SET, null, new BasicEList(constraintList)));
					setHasChanged(true);
				}
			}
		});
		EditingUtils.setID(editConstraint, DeploymentViewsRepository.PlanLocality.Properties.constraint);
		EditingUtils.setEEFtype(editConstraint, "eef::MultiValuedEditor::browsebutton"); //$NON-NLS-1$
		return parent;
	}



	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionListener#firePropertiesChanged(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
	 * @generated
	 */
	public void firePropertiesChanged(IPropertiesEditionEvent event) {
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.PlanLocalityPropertiesEditionPart#getConstraint()
	 * @generated
	 */
	public EList getConstraint() {
    return constraintList;
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.PlanLocalityPropertiesEditionPart#setConstraint(EList newValue)
	 * @generated
	 */
	public void setConstraint(EList newValue) {
    constraintList = newValue;
    if (newValue != null) {
      constraint.setText(constraintList.toString());
    } else {
      constraint.setText(""); //$NON-NLS-1$
    }
  }

	public void addToConstraint(Object newValue) {
		constraintList.add(newValue);
		if (newValue != null) {
			constraint.setText(constraintList.toString());
		} else {
			constraint.setText(""); //$NON-NLS-1$
		}
	}

	public void removeToConstraint(Object newValue) {
		constraintList.remove(newValue);
		if (newValue != null) {
			constraint.setText(constraintList.toString());
		} else {
			constraint.setText(""); //$NON-NLS-1$
		}
	}







	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.emf.eef.runtime.api.parts.IPropertiesEditionPart#getTitle()
	 * @generated
	 */
	public String getTitle() {
    return DeploymentMessages.PlanLocality_Part_Title;
  }



}
